# -*- coding:utf-8 -*-
"""Created By Corvo"""

import time

from const import *
from ..system.base import *

if __name__ == '__main__':
    from server import PingModuleServer


class PingModuleClient(ModuleClientBase):
    """网络诊断模块客户端"""
    __mVersion__ = 1
    __identifier__ = ModuleEnum.identifier

    def __init__(self):
        super(PingModuleClient, self).__init__()
        self.rpc = self.ModuleSystem.CreateRpcModule(self, ModuleEnum.identifier)
        self.update_timer = self.game_comp.AddRepeatedTimer(1, self.OnUpdateSecond)

    def OnDestroy(self):
        self.game_comp.CancelTimer(self.update_timer)
        del self.update_timer
        self.rpc.Discard()
        del self.rpc
        super(PingModuleClient, self).OnDestroy()

    @property
    def server(self):
        # type: () -> PingModuleServer
        return self.rpc

    def server_recall(self, recall):
        # type: (any) -> PingModuleServer
        return self.rpc(recall)

    # -----------------------------------------------------------------------------------

    """触发相关"""

    # 秒更新
    def OnUpdateSecond(self):
        """秒更新"""
        client_time = time.time()

        def recall(server_time):
            print "network: %sms" % int((server_time - client_time) * 1000)

        self.server_recall(recall).OnCheckServerTime()
